<!DOCTYPE html>
<html>
<head>
	<title>机构权限维护</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
	    var path = window.document.location.href;
		var pathName = window.document.location.pathname;
		var hostPath = path.substring(0, path.indexOf(pathName));
		var projectName = pathName.substring(0, pathName.substr(1).indexOf("/") + 1);
		document.write('<scr' + 'ipt src="' + projectName + '/js/html/header.js"></scr'+'ipt>');
    </script>
</head>
<body>
<div style="width:100%"> 

	<div style="float:left;width:49%">
		<div id="dg-buttons" style="height:auto">
			<div style="margin-bottom:5px">
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" 	onclick="saveReso()">保存</a>
			</div>
		</div>
		<ul id="orgTree"></ul> 
	</div>
	
	<div style="float:left">
		<ul id="resoTree"></ul> 
	</div>  
 </div>

<script type="text/javascript">
	$(function(){
	    $('#orgTree').tree({
	    	url : projectName+'/system/orginfo/tree',
	    	onClick:function(node){
	        	treeReLoad(node['id']);  // alert node text property when clicked
	    	}
        });
	});
	$(function(){
	    $('#resoTree').tree({
	    	url : projectName+'/authset/org/resource?orgId='+0,
	        checkbox:true
        });
	});
	
	
	//重新加载权限树
	function treeReLoad(orgId){
        $.messager.progress();
		$('#resoTree').tree("options").url=projectName+'/authset/org/resource?orgId='+orgId;
        $('#resoTree').tree("reload");
        $.messager.progress("close");
	}
	//保存修改后的权限树
	function saveReso(){
        $.messager.progress();
		var roleID;
		var rowData=$('#orgTree').tree('getSelected');
		if(rowData!=null){
			orgId=rowData['id'];
		}else{
			orgId=1;
		}
		//获取新增节点，新增节点是被选中但是没有keyID的
		var nodes=$('#resoTree').tree('getChecked');
		var nodeList=new Array();
		for (var i=0;i<nodes.length;i++){
			var node=new Object();
			var tmp=nodes[i];
			if((tmp.attributes.keyID==0 || typeof(tmp.attributes.keyID)=="undefined")){
				node.id=tmp.attributes.keyID;
				node.resId=tmp.id;
				node.orgId=orgId;
				nodeList.push(node);
			}
		}
		
		//未选中但有主键的为需要删除的
		var dnodes=$('#resoTree').tree('getChecked','unchecked');
		var deleteNodes=new Array();
		var deleteNode=new Object();
		for (var i=0;i<dnodes.length;i++){
			var tmp=dnodes[i];
			if(typeof(tmp.attributes.keyID)!="undefined" && tmp.attributes.keyID!=0){
				var node=new Object();
				node.id=tmp.attributes.keyID;
				node.resId=tmp.id;
				node.orgId=orgId;
				deleteNodes.push(node);
			}
		}
		if(jQuery.isEmptyObject(nodeList) && jQuery.isEmptyObject(deleteNodes)){
			$.messager.alert("系统提示", "权限无变更，无需保存！");
			return false;
		}
		$.ajax({
			type:"POST",
			url:projectName+"/authset/org/resource/update",
			data:{
				"insertNodes":JSON.stringify(nodeList),
				"deleteNodes":JSON.stringify(deleteNodes)
			},
			dataType : "json",
			success:function(data){
				$.messager.alert("系统提示", "保存成功！");
				treeReLoad(orgId);
			}
        });
        $.messager.progress("close");
	}
	
	</script>
</body>
</html>